package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.AdminRoleDomain;
import com.ngc.mall.domain.MenuDomain;
import com.ngc.mall.domain.PermissionMenuDomain;
import com.ngc.mall.service.IMenuService;
import com.ngc.mall.service.IPermissionMenuService;
import com.ngc.mall.service.IRoleService;
import com.ngc.mall.service.IUserService;
import com.ngc.mall.vo.MenuPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单
 *
 * @author: chenxiaolin
 * @date 2023/4/4 8:57
 **/
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单")
public class MenuController {
    @Resource
    private IMenuService iMenuService;

    @Resource
    private IRoleService iRoleService;

    private IPermissionMenuService iPermissionMenuService;

    @Resource
    private IUserService iUserService;

    @GetMapping("/list")
    @ApiOperation("获取所有菜单列表")
    public ResponseResult<List<MenuDomain>> selectAllMenu() {
        return ResponseResult.success(this.iMenuService.selectAllMenu());
    }

    @GetMapping("/list/show")
    @ApiOperation("获取启用的菜单列表")
    public ResponseResult<List<MenuDomain>> findMenuIsShow(){
        return ResponseResult.success(this.iMenuService.findMenuIsShow());
    }

    @GetMapping("/list/myself")
    @ApiOperation("获取当前登录用户的权限菜单")
    public ResponseResult<List<MenuDomain>> findMenuByMySelf(){
        Long uid= LocalUser.getLocalUser().getId();
        List<Long> roleIdList=iUserService.getRoleIdListByUid(uid);
        return ResponseResult.success(this.iMenuService.findMenuByMySelf(roleIdList));
    }


    /**
     * 新建菜单
     */
    @PostMapping("")
    public ResponseResult<Long> createMenu(@RequestBody MenuDomain menuDomain){
        return ResponseResult.success(this.iMenuService.createMenu(menuDomain));
    }

    @PutMapping("/id/{id}")
    @ApiOperation("编辑菜单")
    public ResponseResult<Boolean> updateMenuById(@PathVariable Long id,@RequestBody MenuDomain menuDomain){
        return ResponseResult.auto(iMenuService.updateById(id,menuDomain));
    }


    @DeleteMapping("/id/{id}")
    @ApiOperation("删除菜单（对应删除其子菜单)")
    public ResponseResult<Boolean> deleteMenuById(@PathVariable Long id){
        return ResponseResult.auto(iMenuService.removeById(id));
    }


    @GetMapping("/list/completed")
    @ApiOperation("获取所有菜单及其权限(包含未启用）")
    public ResponseResult<List<MenuPermissionVO>> selectAllMenuWithPermissions(){
        return ResponseResult.success(this.iMenuService.selectAllMenuWithPermissions());
    }


    @PutMapping("/{id}/enable")
    @ApiOperation("启用菜单")
    public ResponseResult<Boolean> updateStatusToEnabled(@PathVariable Long id){
        return ResponseResult.auto(this.iMenuService.update(Wrappers.<MenuDomain>lambdaUpdate().eq(MenuDomain::getId,id).set(MenuDomain::getIsShow,true)));
    }


    @PutMapping("/{id}/disable")
    @ApiOperation("停用菜单(并停用其子菜单）")
    public ResponseResult<Boolean> updateStatusToDisabled(@PathVariable Long id){
        Boolean result;
        result=this.iMenuService.update(Wrappers.<MenuDomain>lambdaUpdate().eq(MenuDomain::getPid, id).set(MenuDomain::getIsShow, false));
        result=this.iMenuService.update(Wrappers.<MenuDomain>lambdaUpdate().eq(MenuDomain::getId, id).set(MenuDomain::getIsShow, false));
        return ResponseResult.auto(result);
    }


    @GetMapping("/{roleId}")
    @ApiOperation("返回指定角色可访问的菜单组")
    public ResponseResult<List<MenuDomain>> getMenusByRoleId(@PathVariable Long roleId){
        AdminRoleDomain roleDomain =iRoleService.getDetailById(roleId);
        List<Long> permissions=roleDomain.getPermissions();
        List<MenuDomain> menuDomainList=new ArrayList<>();
        for(Long permissionId :permissions){
            Long menuId=iPermissionMenuService.getOne(Wrappers.<PermissionMenuDomain>lambdaQuery().eq(PermissionMenuDomain::getPermissionId,permissionId)).getMenuId();
            MenuDomain menuDomain=iMenuService.getById(menuId);
            if(menuDomain.getIsShow()){
                menuDomainList.add(menuDomain);
            }
        }
        return ResponseResult.success(menuDomainList);
    }


}
